package com.eep.order;

import com.eep.common.Customer;
import com.eep.common.Status;
import com.eep.security.Session;
import com.eep.data.DataService;
import com.eep.data.DataServiceImpl;
import com.eep.data.DatabaseName;
import com.eep.logging.LogEvent;
import com.eep.logging.LogEventType;
import com.eep.logging.LogType;
import com.eep.logging.LoggingService;
import com.eep.logging.LoggingServiceImpl;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Calendar;

public class OrderServiceImpl implements OrderService {

    private DataService dataService = null;

    public OrderServiceImpl() {
        dataService = new DataServiceImpl();
        dataService.createConnection(DatabaseName.OrderInfo);
    }

    public Status submitOrder(Session session, OrderDetailed orderDetailed) {

        Status returnStatus = new Status();
        returnStatus.setSuccess(false);
        int order_id = -1;
        if (session.getStatus() == Session.SecurityStatus.applicationAccessGranted) {

            Calendar calendar = Calendar.getInstance();
            String orderTableName = "order" + String.valueOf(calendar.getTimeInMillis());
            DateFormat df = new SimpleDateFormat("MM/dd/yyyy HH:mm:ss");
            String timeStamp = df.format(calendar.getTime());
            orderDetailed.setDate(timeStamp);
            orderDetailed.setOrderTable(orderTableName);

            order_id = dataService.createOrder(orderDetailed);
            
            LoggingService logging = new LoggingServiceImpl();
            logging.logAppEvent(new LogEvent(session.getUsername(), LogType.Order, LogEventType.OrderSubmitted,order_id));
                    
            returnStatus.setSuccess(true);
        } else {
            returnStatus.setMessage("Invalid Session");
        }
        return returnStatus;


    }
}
